import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../util/dimens.dart';
const _kDefaultNavBarBorder = Border(
  bottom: BorderSide(
    color: Color(0x4C000000),
    width: 0.0, // One physical pixel.
    style: BorderStyle.solid,
  ),
);

class CNavigatorBar {
  //topbar
  final Widget leading;
  final Widget middle;
  final Widget trailing;
  final Color topBarBgColor;
  final String title;
  final String rightText;
  final TextStyle rightStyle;
  final Color rightColor;
  final Widget drawer;
  final VoidCallback rightClick;
  final VoidCallback leftClick;
  final Color backColor;
  final bool showBack;
  final bool canResize;
  final Border border;
  final Map<String, WidgetBuilder> routes;

  const CNavigatorBar(
      {this.title,
      this.leading,
      this.middle,
      this.trailing,
      this.rightText,
      this.rightClick,
      this.drawer,
      this.leftClick,
      this.backColor,
      this.rightColor,
      this.canResize = true,
      this.border = _kDefaultNavBarBorder,
      this.showBack = true,
      this.routes = const <String, WidgetBuilder>{},
      this.topBarBgColor = Colors.white,
      this.rightStyle});

  CNavigatorBar.create(
      {this.title,
        this.leading,
        this.middle,
        this.trailing,
        this.rightText,
        this.rightClick,
        this.drawer,
        this.leftClick,
        this.backColor,
        this.rightColor,
        this.canResize = true,
        this.border = _kDefaultNavBarBorder,
        this.showBack = true,
        this.routes = const <String, WidgetBuilder>{},
        this.topBarBgColor = Colors.white,
        this.rightStyle});

  CupertinoNavigationBar build(context) {
    return CupertinoNavigationBar(
      border: border,
      automaticallyImplyMiddle: title != null || middle != null,
      automaticallyImplyLeading: leading != null && showBack,
      transitionBetweenRoutes: false,
      previousPageTitle: title,
      leading: showBack ? _getLeading(context) : null,
      trailing: _getTrailing(context),
      middle: _getMiddle(context),
      backgroundColor: topBarBgColor,
    );
  }

  Widget _getMiddle(BuildContext context) {
    if (middle != null) {
      return middle;
    }
    if (title == null) {
      return null;
    }
    return Text(
      title,
      style: TextStyle(fontSize: sp(18), fontWeight: FontWeight.w500),
    );
  }

  Widget _getLeading(BuildContext context) {
    if (leading != null) {
      return leading;
    }
    return CupertinoButton(
      onPressed: leftClick ??
          () {
            Navigator.maybePop(context);
          },
      padding: EdgeInsets.all(0),
      child: Icon(
        CupertinoIcons.back,
        color: backColor ?? CupertinoColors.black,
        size: 24,
      ),
      pressedOpacity: 0.6,
      minSize: 25,
    );
  }

  Widget _getTrailing(BuildContext context) {
    if (trailing != null) {
      return trailing;
    }
    if (rightClick != null && rightText != null) {
      return CupertinoButton(
        onPressed: rightClick,
        //pressedOpacity=0.6,
        padding: EdgeInsets.all(0),
        child: Text(
          rightText,
          style: rightStyle ??
              TextStyle(
                  fontSize: dip(15),
                  color: rightColor ?? CupertinoColors.activeBlue),
        ),
        pressedOpacity: 0.6,
        minSize: 25,
      );
    }
    return null;
  }
}
